/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

/**
 *
 * @author Alexx
 */

import engine.Singleton;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import listener.MyListener;
import listener.MyrealListener;
import listener.Proxy;
import org.json.simple.parser.ParseException;
import protocol.Protocol;



public class ServeOneThread extends Thread
{
  
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private static List<Player> clients;
    public MyListener listener;

  
    public Player p;
   // private String login = null;
    //public Protocol p;
    
    
    
    
    public ServeOneThread() throws IOException
    {
    }
    
    public ServeOneThread(Socket s) throws IOException
    {
        socket = s;
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                .getOutputStream())), true);
        p = new Player(this);
        Singleton.clients.add(p); 
        listener = new Proxy();
        
        start();
    }
    
    public  byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        char[] cbuf = new char[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
            cbuf[i] = (char) b[i];
     
        }
        return b;
    }

      
    public void sendUser(String send) throws IOException
    {
      int n = send.length();
      String len = Integer.toString(n);
      for (int i=len.length(); i<7; i++){
        len = "0" + len;
      }
      send = len + send;
    //  out.print(n);
    //  byte[] b = intToByteArray(35874);
//      BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//      output.write(b);
      //send = b[0] + b[1] + b[2] + b[3] + send;
      System.out.println("Server out " + send);
      out.print(send);
      out.flush();
        
    }
     
    
    public void run()
    {
        try
        {
            while (true)
            {
                String str = in.readLine();

                    
                System.out.println("Server comming: " + str);
        try {
                    try {
                        new Protocol().get(str, this); /////////////////////
                    } catch (SQLException ex) {
                        Logger.getLogger(ServeOneThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
        } catch (ParseException ex) {
          Logger.getLogger(ServeOneThread.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        
              if (str.equals("{\"Type\": \"End\"}")) // тут имя клиента
              {
               // Singleton.getInstance().delUser(login);
                out.println("{\"Type\": \"End\"}");
                break;
              }
                if(str.length() > 1){
                   str = "";
                }
            }
            System.out.println("closing...");
        }
        catch (IOException e)
        {
            System.err.println("IO Exception");
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch (IOException e)
            {
                System.err.println("Socket not closed");
            }
        }
    }

    
    
}
